<template>
    <div>
        <el-card class="box-card" shadow="never">
            <!--导航栏-->
            <el-page-header @back="back" :content="content">
            </el-page-header>
            <el-form ref="form" class="edit-el-form" label-position="right" size="small" :model="form" :rules="rules" label-width="auto">
                <el-row :gutter='12'>
                  <el-col :span='11'>
                    <el-form-item label="所属社区" prop="c_id">
                        <el-select v-model="form.c_id" @change="handleComChange" placeholder="请选择社区">
                            <el-option
                                    v-for="item in basic_data"
                                    :key="item.c_id"
                                    :label="item.c_name"
                                    :value="item.c_id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span='11'>
                    <el-form-item label="所属网格" prop="g_id">
                        <el-select v-model="form.g_id" placeholder="请选择网格">
                            <el-option
                                    v-for="item in allgridlist"
                                    :key="item.g_id"
                                    :label="item.g_name"
                                    :value="item.g_id">
                            </el-option>
                         </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                    <el-form-item label="职责" prop="duty">
                        <el-select v-model="form.duty" placeholder="请选择职责">
                            <el-option
                                    v-for="item in allserviceduty"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-form-item label="负责人姓名" prop="name">
                        <el-input v-model="form.name" required></el-input>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-form-item label="负责区域" prop="duty_area">
                        <el-input v-model="form.duty_area"></el-input>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-form-item label="电话" prop="phone">
                        <el-input v-model="form.phone" maxlength="11" show-word-limit></el-input>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-form-item label="照片" prop="photo">
                       <Upload :limit="1" :file_list="photo_list" :catch_urls="catch_urls" :remove_urls="remove_urls" :upload_tip="upload_tip"></Upload>
                    </el-form-item>
                </el-row>

                <el-form-item label="状态">
                    <el-radio-group v-model="form.enabled">
                        <el-radio border label="0">禁用</el-radio>
                        <el-radio border label="1">正常</el-radio>
                    </el-radio-group>
                </el-form-item>

                <!--查看不展示按钮-->
                <el-form-item v-if="edit_type!=0">
                    <el-button type="primary" @click="submitForm('form')" :loading="loading">{{button_name}}</el-button>
                    <el-button @click="back">取消</el-button>
                </el-form-item>
            </el-form>
        </el-card>

    </div>
</template>

<script>
    import Upload from '@/components/Upload.vue';
    import { getServiceTeamInfo,editServiceTeam } from '@/api/serviceteam.js';
    import { getBasicData } from '@/api/basicdata.js';
    export default {
        name:'ServiceTeamEdit',
        components:{ Upload },
        data() {
            return {
                form: {
                    g_id:'',
                    c_id:'',
                    name: '',
                    duty:'',
                    duty_area:'',
                    phone:'',
                    photo:'',
                    enabled: '1',
                },
                //校验规则
                rules:{
                    name: [
                        { required:true , message:'请输入负责人姓名',trigger:'blur' }
                    ],
                    c_id:[
                        { required:true , message:'请选择所属社区'}
                    ],
                    g_id:[
                        { required:true , message:'请选择所属网格'}
                    ],
                    duty:[
                        { required:true , message:'请选择职责'}
                    ],
                    phone:[
                        { required:true , message:'请输入电话'},
                    ],
                    photo:[
                        { required:true , message:'请上传照片'}
                    ]
                },
                //加载组件
                loading: false,
                button_name:'立即提交',
                content:'服务团队添加',
                allserviceduty:this.$store.state.allserviceduty,
                photo_list:[],
                image_urls:[],
                upload_tip:'只能上传jpg/png/gif文件，且不超过500kb，仅允许上传一张',
                allgridlist:[],
                basic_data:[]
            }
        },
        methods: {
            submitForm(formName){
                let that = this;
                    this.$refs[formName].validate((valid) => {
                            if (valid) {
                                that.loading=true;
                                that.button_name='正在提交';
                                if(that.image_urls.length>0){
                                   that.form.photo=that.image_urls[0];
                                }
                                var send_data={
                                    api_token:that.$store.getters.getSessionId,
                                };
                                //写入表单内容
                                for(let item in that.form){
                                    send_data[item]=that.form[item];
                                }
                                if(that.id!=-1){
                                    send_data.id=that.id;
                                }
                                editServiceTeam(send_data).then(function(response){
                                     let code=response.data.code;
                                     let type=code==1?'success':'error';
                                     let msg=code==1?'成功':'失败';
                                     that.$message({
                                         message:that.content+msg,
                                         duration:1200,
                                         type:type,
                                         onClose:function(e){
                                             that.loading=false;
                                             if(code==1){
                                                that.$router.push('/basicdata/serviceteamlist');
                                             }else{
                                                that.button_name='立即提交';
                                             }
                                         }
                                    })
                                })
                            }else{
                                setTimeout(()=>{
                                  const is_error=document.getElementsByClassName('is-error');
                                  is_error[0].querySelector('input,button').focus();
                                },100);
                                return false;
                            }
                    })
            },
            back(){
                this.$router.push({
                   name:'serviceteamlist',
                   query:{
                     g_id:this.$store.getters.getCurrentGId
                   }
                });
            },
            handleComChange(c_id){
                this.form.g_id='';
                this.getGridList(c_id);
            },
            getGridList(c_id){
              for(let i=0;i<this.basic_data.length;i++){
                  if(c_id==this.basic_data[i]['c_id']){
                    this.allgridlist=this.basic_data[i]['children'];
                    break;
                  }
              }
            },
            catch_urls(url){
              this.image_urls.push(url);
            },
            remove_urls(remove_index){
              this.image_urls.splice(remove_index,1);
            },
            getServiceTeamInfo(){
              if(this.id!=-1){
                  let send_data={
                      api_token:this.$store.getters.getSessionId,
                      id:this.id
                  }
                  let that=this;
                  getServiceTeamInfo(send_data).then(function(response){
                      if(response.data.code==1){
                        let val=response.data.info;
                        that.image_urls=[val.photo];
                        that.photo_list.push({
                            name:0,
                            url:val.photo
                        });
                        if(val.c_id>0){
                            that.getGridList(val.c_id);
                        }
                        for(let item in that.form){
                          if(item=='duty'){
                            that.form[item]=val[item].toString()==0?'':val[item].toString();
                          }else if(item=='enabled'){
                            that.form[item]=val[item].toString();
                          }else{
                            that.form[item]=val[item];
                          }
                        }
                      }
                  })
              }
            }
        },
        created(){
            if(this.edit_type==1){
                this.content='服务团队添加';
            }else if(this.edit_type==0){
                this.content='服务团队详情';
            }else if(this.edit_type==2){
                this.content='服务团队编辑';
            }
            let send_data={
               api_token:this.$store.getters.getSessionId
            };
            let that=this;
            getBasicData(send_data).then(function(response){
               that.basic_data=response.data.list;
               that.getServiceTeamInfo();
            })
        },
        computed:{
            id(){
                return this.$route.params.id;
            },
            edit_type(){
                return this.$route.params.edit_type;
            }
        }
    }
</script>

<style>
    .edit-el-form{
        margin-top:30px;
    }
</style>
